package org.cfx.auth.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.cfx.auth.domain.dto.RoleDto;
import org.cfx.auth.service.IAuthRoleService;
import org.cfx.auth.domain.AuthRole;
import org.cfx.auth.query.AuthRoleQuery;
import org.cfx.basic.annotation.Permission;
import org.cfx.basic.util.AjaxResult;
import org.cfx.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/authRole")
@Permission(name = "角色权限")
public class AuthRoleController {
    @Autowired
    public IAuthRoleService authRoleService;


    /**
     * 保存和修改公用的
     * @param authRole  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody AuthRole authRole){
        try {
            if( authRole.getId()!=null)
                authRoleService.update(authRole);
            else
                authRoleService.add(authRole);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            authRoleService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            AuthRole authRole = authRoleService.queryById(id);
            return AjaxResult.me().setData(authRole);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }

    @GetMapping("/permission/{id}")
    public AjaxResult getPermission(@PathVariable("id")Long id)
    {
        try {
            List<Long> permissionByRoleId = authRoleService.loadPermissionByRoleId(id);
            return AjaxResult.me().setData(permissionByRoleId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< AuthRole> list = authRoleService.queryAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody AuthRoleQuery query)
    {
        try {
            PageList<AuthRole> pageList = authRoleService.queryAllPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @DeleteMapping("/deleteList")
    @Operation(summary = "批量删除")
    public AjaxResult deleteList(@RequestBody ArrayList<Long> ids) {
        try {
            authRoleService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false);
        }
    }

    @PutMapping("/save")
    @Permission(name = "权限的添加或修改")
    public AjaxResult save(@RequestBody RoleDto roleDto){
        try {
            System.out.println(roleDto);
            authRoleService.saveRoleAndPermission(roleDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PutMapping("/update")
    @Permission(name = "权限的添加或修改")
    public AjaxResult update(@RequestBody RoleDto roleDto){
        try {
            authRoleService.updateRoleAndPermission(roleDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
